package com.lcq.gulimall.authsever.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.parser.ParserConfig;
import com.lcq.common.constant.AuthConstant;
import com.lcq.common.exception.BizCodeEnum;
import com.lcq.common.utils.R;
import com.lcq.common.vo.MemberRsepVo;
import com.lcq.common.vo.UserShowInfoVo;
import com.lcq.gulimall.authsever.feign.MemberFeign;
import com.lcq.gulimall.authsever.vo.LoginVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

@Controller
public class LoginWebController {
    @Autowired
    MemberFeign memberFeign;
    @PostMapping("/auth/login")
    public String login(LoginVo loginVo, RedirectAttributes redirectAttributes, HttpSession session){
        BizCodeEnum.MEMBER_USERNAME_PASSWORD_INVALID.getCode();
        R result = memberFeign.login(loginVo);
        Integer resCode = (Integer) result.get("code");
        if (!resCode.equals(0)){
            redirectAttributes.addFlashAttribute("errMsg",result.get("msg"));
            return "redirect:http://auth.gulimall.com/login.html";
        }
//        String json = (String) result.get(AuthConstant.LOGIN_USER);
//        ParserConfig parserConfig = new ParserConfig();
//        BeanUtils.copyProperties(ParserConfig.getGlobalInstance(),parserConfig);
//        parserConfig.setAutoTypeSupport(true);
//        UserShowInfoVo user = (UserShowInfoVo) JSON.parse(json,parserConfig);
        MemberRsepVo user = (MemberRsepVo) result.getObject(AuthConstant.LOGIN_USER);
        session.setAttribute(AuthConstant.LOGIN_USER,user);
        return "redirect:http://gulimall.com";
   }
}
